package com.xxx.corejava.day1217;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCount {
    private JFrame jf;
    private JLabel jl;
    private JButton jb1;
    private JButton jb2;
    private int count;

    public TestCount() {
        jf=new JFrame();
        jl=new JLabel(count+"");
        jb1=new JButton("+");
        jb2=new JButton("-");
    }

    private void init(){
        jf.setLayout(new FlowLayout());
        jf.add(jl);
        jf.add(jb1);
        jf.add(jb2);
    }

    private void addEventHandler(){
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("+");
                jl.setText(++count+"");
            }
        });

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("-");
                jl.setText(--count+"");
            }
        });
    }

    public void show(){
        init();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestCount().show();
    }
}
